from django.core.cache import cache
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from addresses.models import Area


class AreasView(View):
    #城区列表
    def get(self, request):
        area_list = cache.get('areas_list')
        if not area_list:
            try:
                areas = Area.objects.all()
            except Exception as e:
                return JsonResponse({'errno': 4001, 'errmsg': '数据库查找失败'})

            area_list = []
            for area in areas:
                area_list.append({'aid':area.id, 'aname': area.name})

            cache.set('area_list', area_list, 3600)

        return JsonResponse({'errno': 0, 'errmsg': 'ok', 'data': area_list})